<template>
   
        <div>
                <el-form ref="queryForm" :model="queryForm" class="formEmbedTable mt20" :rules="rules">
                    <table>
                        <caption>Add</caption>
                        <tr>
                            <td>A/C No.</td>
                            <td>
                                <el-form-item  prop="accNo">
                                    <el-input v-model="queryForm.accNo" @blur="queryAccStt"></el-input>
                                </el-form-item>              
                            </td>
                        </tr>	
                        <tr>
                            <td>A/C Type</td>
                            <td>
                                <el-form-item  prop="accType">
                                    <el-select v-model="queryForm.accType">
                                        <template v-for="item in typeOptions">
                                            <el-option :label="item.label" :value="item.value"></el-option>
                                        </template>         
                                    </el-select>
                                </el-form-item>             
                            </td>
                        </tr>
                        <tr>
                            <td>transfer cycle limit</td>
                            <td>
                                <el-form-item  prop="transLimit">
                                    <el-input v-model="queryForm.transLimit" :disabled="true"></el-input>
                                </el-form-item>             
                            </td>
                        </tr>
                        <tr>
                            <td>HKD Bill Payment Cycle Limit</td>
                            <td>
                                <el-form-item  prop="payLimit">
                                    <el-input v-model="queryForm.payLimit" :disabled="true"></el-input>
                                </el-form-item>             
                            </td>
                        </tr>
                        <tr>
                            <td>Fixed Deposit Cycle Limit</td>
                            <td>
                                <el-form-item  prop="depositLimit">
                                    <el-input v-model="queryForm.depositLimit" :disabled="true"></el-input>
                                </el-form-item>             
                            </td>
                        </tr>
                        	
                    </table>
                </el-form>
                <div class="btnArea mt20 tac">
                    <el-button type="primary" @click="queryAuth()">Confime</el-button>
                    <el-button type="primary" @click="back()">Back</el-button>
                </div>

                <!--成功-->
                <el-dialog :visible.sync="addAccDialogVisible" size=tiny class="tac" :close-on-click-modal="false" :close-on-press-escape="false">
                    <div class="tableForm mt20">
                        <div class="transactionStatus">
                            <ynet-icon Type="transactionStatus" NO="1"></ynet-icon>
                            <span class="ml15">Adding a hang operation successful</span>
                        </div>
                        <div class="btnArea mt20 tac">
                            <el-button type="primary" @click="addAccDialogVisible = false,addConfimEnter()">Confirm</el-button>
                        </div>
                    </div> 
                </el-dialog>
                <!--成功-->

        </div>

</template>

<script>
	export default {
		name:'systemAdd',
        props: {
            toPrimaryAcc: {type: String, default:''},
            toAccoType: {type: String, default:''},
            toUserNo: {type: String, default:''},
        },
		components:{
		},
        
		data(){
            return { 
               rules:{
                    accNo: [
                        {validator:this.validatorAcc, trigger:'blur'}
                    ],
                    accType: [
                        {required:true,message:'Can not be enpty',trigger:'change'}
                    ],
                    
               } ,
               typeOptions:[
                    {value:'S',label:'定期'},
                    {value:'P',label:'活期'},
                ],
               queryForm:{
                    accNo:'',
                    accType:'',
                    transLimit:'',
                    payLimit:'',
                    depositLimit:'',
                },
                currency:'',
                addAccDialogVisible:false,
                T24AccountInfoList:[],
                needAuth:'',
                authType:'',
            }   
        },
        mounted(){
            let _this = this
            console.log("_this.toAccoType",_this.toAccoType)
            _this.queryForm.accType = _this.toAccoType
            _this.$tools.request(_this,"IM040103.do",{}).then(   //查银行限额
                data=>{
                    let errorCode = data.body.errorCode
                    let errorMsg = "System Error Try again";
                    if(errorCode =='0'){
                        let limitType = data.body.limitType
                    if(limitType){
                        let limitList = []
                        if(limitType == 'A'){//账户限额
                            limitList = data.body.acctLimit || []
                            for(let i = 0; i < limitList.length; i++){
                                let obj = limitList[i]
                                if(obj.TBL_LIMITTYPE == 'P')  _this.queryForm.transLimit = obj.TAL_DAY_LIMIT
                                if(obj.TBL_LIMITTYPE == 'T')  _this.queryForm.payLimit = obj.TAL_DAY_LIMIT
                                if(obj.TBL_LIMITTYPE == 'F')  _this.queryForm.depositLimit = obj.TAL_DAY_LIMIT
                            }
                        }
                        if(limitType == 'B'){//银行限额
                            limitList = data.body.bankLimit || []
                            for(let i = 0; i < limitList.length; i++){
                                let obj = limitList[i]
                                if(obj.TBL_LIMITTYPE == 'P')  _this.queryForm.transLimit = obj.TBL_DAY_LIMIT
                                if(obj.TBL_LIMITTYPE == 'T')  _this.queryForm.payLimit = obj.TBL_DAY_LIMIT
                                if(obj.TBL_LIMITTYPE == 'F')  _this.queryForm.depositLimit = obj.TBL_DAY_LIMIT
                            }
                        }
                    }
                    }else{
                    _this.$alert(errorMsg, 'Error', {confirmButtonText: 'OK'});
                    }
                }
            )
        },
		methods:{ 
            back(){
                this.$emit('back')
            },
            //账号校验
           validatorAcc(rule, value, callback){
                let _this = this
                if("" == value || undefined == value) return callback("Please input account number")
                if(!(/^[0-9]*$/.test(value))){
                    return callback("Please enter the correct format of the account !") 
                }
                callback()
            },
            queryAuth(){//查询授权模式
            console.log("查询授权模式")
                let body={};body.bsnCode="IM040201";body.version="IM040201",
                this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                    if(data.body.errorCode==0){
                        if(data.body.needAuth==0){//无需授权
                            this.needAuth=0;
                            this.addAccToMainPhoneAcc()
                            
                        }else if(data.body.needAuth==1 && data.body.authType==1){  // 临桂式授权
                            this.needAuth=1;
                            this.authType=1;
                            this.addAccToMainPhoneAcc()
                            
                            // this.$refs["dialogTable"].show()
                        }else if( data.body.needAuth==1 && data.body.authType==2 ){   //一录一审
                            this.needAuth=1;
                            this.authType=2;
                            this.addAccToMainPhoneAcc()
                        }
                    }else{
                        this.$alert("System Error",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })   
			},
            //查询该账号是否已经加挂
            queryAccStt(){ 
                let _this = this
                let queryData = {}
                queryData.queryAcc = _this.queryForm.accNo
                queryData.primaryAcc = _this.toPrimaryAcc
                _this.$refs.queryForm.validate((valid)  => {
                  if(valid){
                        _this.$tools.request(_this,"IM080203Action.do",queryData).then(data=>{
                        if(data.body.errorCode == '0'){
                                let status = data.body.status;
                                if('0' == status || '1' == status){
                                    this.$alert('This account has been added !',{
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    })
                                }else{
                                    _this.validatorAccT24()
                                }
                        }else{
                            this.$alert("System Error Try again", "warning",{
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            })
                        }
                    })
                  }
                })
            },
            //验证T24返回账户状态
            validatorAccT24(){
               let _this = this
               let queryData = {}
               queryData.ACCTIDLIST = []
               queryData.ACCTIDLIST.push({acc_no:_this.queryForm.accNo})
               _this.$tools.request(_this,"IM080204Action.do",queryData).then(data=>{
                   if(data.body.errorCode == '0'){
                        console.log("data.body.ACCOUNTLIST",data.body.ACCOUNTLIST)
                        _this.T24AccountInfoList = data.body.ACCOUNTLIST
                        if('Y' == data.body.ACCOUNTLIST[0].inactiv_marker || 'N' == data.body.ACCOUNTLIST[0].ind_acc_exist){
                           _this.$alert('T24 returns the account status verification result:refuses to add and hang !',{
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            })
                        }
                        // if(){   //根据接口返回POSTING.RESTRICT 的值判断是否允许加挂
                        //     _this.$alert('T24 returns the account status verification result:refuses to add and hang !',{
                        //         confirmButtonText: 'Confirm',
                        //         callback: action => {
                        //         }
                        //     })
                        // }
                        // if('联名账号判定'){  //加挂账号如果为联名账号则该账号下的人员信息必须与基本户保持一致（CUS.ID一致），如果不一致不允许进入确认页面且弹出提示框：加挂账号CUS.ID与主账号不一致。
                        //     _this.$alert('T24 returns the account status verification result:refuses to add and hang !',{
                        //         confirmButtonText: 'Confirm',
                        //         callback: action => {
                        //         }
                        //     })
                        // }  
                        //
                       
                   }else{
                       _this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                   }
               })
           },
           //  账号加挂到电话主账户
           addAccToMainPhoneAcc(){
               let _this = this
               let queryData = {}
               queryData.actionType = '0'
               queryData.bsnCode = 'IM040201'
               queryData.reqTP = 'A'
               queryData.primaryAcc = _this.toPrimaryAcc
               queryData.relateAcc = _this.queryForm.accNo
               queryData.sequenceNo = ''
            //    queryData.openDate =     //下挂时间   从服务中取系统时间
               queryData.cancelDate = ''   //解挂时间
               queryData.accOwnerType = ''   //账户所有者类型
               queryData.accType =    _this.T24AccountInfoList[0].category       //账户类型
               queryData.currency = _this.T24AccountInfoList[0].ccy_code       //币种
               queryData.accRight = ''   //账户权限
               queryData.signType = '0'
               queryData.signChannel = 'CNT'
               queryData.status = '0'
               queryData.channel = '00010000'
               queryData.toUserNo = _this.toUserNo
               queryData.accSubType = _this.queryForm.accType
               queryData.accChName = ''
               queryData.accEnName = ''

               _this.$tools.request(_this,"IM080202Action.do",queryData).then(data=>{
                   if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){  //一录一审
                            this.$message({
                                type:'success',
                                message:'Waiting for Authorized'
                            })
                            
                        } else if(this.needAuth==1 && this.authType==1){  //临柜
                            if(data.body.authStatus==1){
                                this.$message({
                                    type:'warning',
                                    message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                                
                            }else{
                                this.$message({
                                    type:'success',
                                    message:'add Success',
                                    showClose:true,
                                })
                                _this.addAccDialogVisible = true
                                
                            }
                        }else{
                            this.$message({
                                type:'success',
                                message:'Add Success',
                                showClose:true,
                            })
                            _this.addAccDialogVisible = true
                        }
                    } else{
                        this.$alert("System Error Try again", "warning",{
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            })
                    }
               })
           },
           addConfimEnter(){
                this.$emit('back')
            },
		}
	}
</script>